﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FSS.Core.Controls;
using FSS.Core.Util;
using Ext.Net;
using FSS.BLL.Entities;
using FSS.BLL.Services;

namespace SalesManagement.Modules.List
{
    public partial class Measure : BasePage
    {
        public Measure()
            : base(SCREENS.MEASURE)
        {

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            BindingMeasure();
        }
        [DirectMethod]
        public void DocumentReady()
        {
            BindingProduct();
        }
        protected void grdMeasure_OnCommand(object sender, DirectEventArgs e)
        {
            int productid = Util.Obj2Int(e.ExtraParams["ProductId"]);
            int baseunit = Util.Obj2Int(e.ExtraParams["BasicUnit"]);
            int ratiounit = Util.Obj2Int(e.ExtraParams["RatioUnit"]);
            string command = e.ExtraParams["command"];
            if (Constants.Edit.Equals(command))
            {
                this.WindowEditor.ShowEdit(productid, baseunit, ratiounit);
            }
            else if (Constants.Delete.Equals(command))
            {

            }
        }
        public void btnAdd_Click(object sender, DirectEventArgs e)
        {
            int id = Int32.Parse(cboProduct.SelectedItem.Value);
            this.WindowEditor.Show(Int32.Parse(cboProduct.SelectedItem.Value));
        }

        public void storeMeasure_OnRefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            int id = Int32.Parse(cboProduct.SelectedItem.Value);
            BindingMeasurebyProductId(id);
        }
        private void BindingMeasure()
        {
            MeasureEntity measureE = new MeasureEntity();
            MeasureService measureS = MeasureService.GetInstance();
            List<MeasureEntity> lstMeasure = (List<MeasureEntity>)measureS.LoadAll();
            if (lstMeasure != null)
            {
                stProduct.DataSource = lstMeasure;
                stProduct.DataBind();
            }
        }
        private void BindingMeasurebyProductId(int Id)
        {
            MeasureEntity measureE = new MeasureEntity();
            MeasureService measureS = MeasureService.GetInstance();
            measureE.ProductId = Id;
            List<MeasureEntity> listoreMeasure = (List<MeasureEntity>)measureS.LoadByProductId(measureE);
            if (!listoreMeasure.Equals(null))
            {
                storeMeasure.DataSource = listoreMeasure;
                storeMeasure.DataBind();
            }
        }
        protected void BindingProduct()
        {
            ProductEntity productE = new ProductEntity();
            ProductService productS = ProductService.GetInstance();
            List<ProductEntity> listProduct = (List<ProductEntity>)productS.LoadAll();
            if (!listProduct.Equals(null))
            {
                stProduct.DataSource = listProduct;
                stProduct.DataBind();
            }
        }
    }
}
